/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ssm.changeunit.command;

import java.util.LinkedList;
import java.util.Queue;
import com.ssm.changeunit.*;
import com.ssm.changeunit.persistance.DatabaseManager;
import com.ssm.changeunit.persistance.ManagedItemListDAO;
import com.ssm.changeunit.persistance.ParentDAO;
import com.ssm.changeunit.persistance.ParentDTO;
import com.ssm.changeunit.persistance.PersistanceException;
import com.ssm.changeunit.util.ChangeUnitTraveler;
import com.ssm.changeunit.util.FileUtil;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;

/**
 *
 * @author 시리우스
 */
public class LogCommandWorker extends CommandWorker {
    private Logger log = Logger.getLogger(LogCommandWorker.class);
    
    /**
     * 생성자
     */
    public LogCommandWorker() {
    }

    /**
     * 실제 Log를 처리하는 클래스
     * 
     * @param commandWorkerOptions
     * @throws com.ssm.changeunit.command.CommandWorkerException
     */
    @Override
    public void work(CommandWorkerOptions commandWorkerOptions) throws CommandWorkerException {
        // 데이터베이스 체크
        log.debug("데이터베이스가 존재하는지 검사합니다.");
        
        try {
            if(!DatabaseManager.getInstance().isExists())
                throw new CommandWorkerException("데이터베이스가 존재하지 않습니다.");
        }
        catch(PersistanceException ex) {
            throw new CommandWorkerException(ex);
        }
        
        ChangeUnitTraveler changeUnitTraveler = new ChangeUnitTraveler();

        try {
            // 초기화        
            changeUnitTraveler.init();

            while(changeUnitTraveler.hasNext()) {
                ChangeUnit nextChangeUnit = changeUnitTraveler.getNextChangeUnit();

                log.info("--------------");
                log.info("ChangeUnitId: " + nextChangeUnit.getChangeUnitId());
                log.info("Log: " + nextChangeUnit.getLog());
                
                StringBuilder branchStringBuilder = new StringBuilder();
                branchStringBuilder.append("Branch: ");
                
                for(Branch branch : nextChangeUnit.getBranchs()) 
                    branchStringBuilder.append(branch.getBranchName() + " ");
                
                log.info(branchStringBuilder.toString());
                log.info("--------------");
            }
        }
        catch(ChangeUnitException ex) {
            throw new CommandWorkerException(ex);
        }
    }
}
